<%@page import="java.net.URLEncoder"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'doUserCreate.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    <%
    //表单post方式提交，中文乱码处理
    request.setCharacterEncoding("utf-8");
    //response.setCharacterEncoding("utf-8");    
    	//获取用户注册的数据:用户名、密码、email、爱好
    	String userName = request.getParameter("username");
    	 //表单get方式提交，中文乱码处理-治标(如果表单有很多个表单字段，就得写多个)
    	/* byte[] userNames = userName.getBytes("ISO-8859-1");
    	userName = new String(userNames,"utf-8");
    	//合并成一句--》
    	userName = new String(userName.getBytes("ISO-8859-1"),"utf-8"); */
    	
    	String pwd = request.getParameter("password");
    	String email = request.getParameter("email");
    	String hobbys[] = request.getParameterValues("hobby");
     %>
     用户名：
     <%
     if(userName!=null && !userName.equals("")){
	     if(userName.equals("admin")){ 
	     	//用户注册失败，因为该用户已经存在。重新跳转回用户注册页面，重新注册
	     	request.setAttribute("message", "注册失败!"); //一次请求的范围内
	     	//转发
	     	request.getRequestDispatcher("userCreate.jsp").forward(request, response);
	     }else{
	     	//允许注册，注册成功，保存用户信息
	     	session.setAttribute("username", userName);
	     	/* //设置session过期
	     	session.setMaxInactiveInterval(5); */
	     	//使用cookie记录用户名，实现自动填写用户名功能
	     	Cookie cookie = new Cookie("username",userName);
	     	//设置路径
	     	cookie.setPath("/");
	     	cookie.setMaxAge(60*60);
	     	response.addCookie(cookie);
	     	
	     	response.sendRedirect(request.getContextPath()+"/index.jsp");
	     	
	     	/* //注册成功，跳转到首页去
	     	request.setAttribute("message", "注册成功!");
	     	//重定向
	     	response.sendRedirect(request.getContextPath()+"/index.jsp"); */
	     	
	     	//换作用域保存，或者如下写法
	     	//response.sendRedirect(request.getContextPath()+"/index.jsp?info=success");
	     	//如果传递中文提示：
	     	//String info = "成功";
	     	//info = URLEncoder.encode(info,"UTF-8");
	     	//response.sendRedirect(request.getContextPath()+"/index.jsp?info="+info);
	     }
      
     %>
     <%=userName %>
     <%
     }else{
     	out.println("用户名未填写！");
     }
      %>
     <br/>
     用户密码：<%=pwd %><br/>
     用户Email：<%=email %><br/>      
     用户爱好：<br/>
     <%
     if(hobbys!=null && hobbys.length!=0){
     	//用户选择了爱好，将爱好获取并且输出
     	for(String hobby:hobbys){
     		out.println(hobby+"<br/>");
     	}
     }else{
     	out.println("您没有选择任何爱好！");
     }
     
     %><br/>
  </body>
</html>
